package com.github.ilioili.justdoit.components.fragment;

import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

import com.github.ilioili.justdoit.components.activity.DetailActivity;
import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.config.EventConstants;
import com.github.ilioili.justdoit.model.pojo.ScheduleEvent;
import com.github.ilioili.justdoit.util.StatisticUtil;
import com.github.ilioili.justdoit.widget.PieChartView;
import com.taihe.template.base.BaseActivity;
import com.taihe.template.base.BaseFragment;
import com.taihe.template.base.injection.Click;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

/**
 * Created by User on 16/6/18.
 */
@Layout(R.layout.fragment_circle_chart)
public class ChartFragment extends BaseFragment {
    @Id(R.id.tv_sub_info)
    private TextView tvSubInfo;
    @Id(R.id.tv_complete_num)
    private TextView tvCompleteNum;
    @Id(R.id.pieChartView)
    private PieChartView pieChartView;

    private ObjectAnimator ourterObjectAnimator;
    private ObjectAnimator innerObjectAnimator;
    private ScheduleEvent scheduleEvent;


    /**
     * 圆圈里面的信息
     */
    public void updateCircleInfo(ScheduleEvent scheduleEvent) {
        this.scheduleEvent = scheduleEvent;
        if (null == ourterObjectAnimator) {
            ourterObjectAnimator = new ObjectAnimator();
            ourterObjectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
            ourterObjectAnimator.setDuration(500);
            ourterObjectAnimator.setTarget(pieChartView);
            ourterObjectAnimator.setPropertyName("outerProgress");

            innerObjectAnimator = new ObjectAnimator();
            innerObjectAnimator.setInterpolator(new DecelerateInterpolator());
            innerObjectAnimator.setDuration(500);
            innerObjectAnimator.setTarget(pieChartView);
            innerObjectAnimator.setPropertyName("innerProgress");
        }
        int competeCount = StatisticUtil.getCompeteCount(scheduleEvent.id);
        tvCompleteNum.setText(competeCount + "");
        float timeProgress = 0;
        float completeProgress = 0;
        if (scheduleEvent.type == EventConstants.TYPE_HABIT) {//习惯
            int totalCount = StatisticUtil.getTotalCount(scheduleEvent, System.currentTimeMillis());
            tvSubInfo.setText(totalCount + "th");
            setGone(R.id.tv_target_title);
            setVisiable(R.id.tv_habit_title);
            if (totalCount == 0) {
                timeProgress = 0;
                completeProgress = 0;
            } else {
                completeProgress = 1f * competeCount / totalCount;
                if(completeProgress>1) completeProgress=1;
                timeProgress = 1;
            }

        } else {
            int elapseCount = StatisticUtil.getTotalCount(scheduleEvent, System.currentTimeMillis());
            int totalCount = StatisticUtil.getTotalCount(scheduleEvent, scheduleEvent.endTime);
            tvSubInfo.setText(elapseCount + "th/" + totalCount);
            setGone(R.id.tv_habit_title);
            setVisiable(R.id.tv_target_title);
            if (totalCount != 0) {
                timeProgress = 1f * elapseCount / totalCount;
                completeProgress = 1f * competeCount / totalCount;
                if(completeProgress>1) completeProgress=1;
                if(timeProgress>1) timeProgress=1;
            } else {
                timeProgress = 0;
                completeProgress = 0;
            }

        }
        if (Math.abs(pieChartView.getOuterProgress() - timeProgress) > 0.1f) {
            ourterObjectAnimator.setFloatValues(pieChartView.getOuterProgress(), timeProgress);
            ourterObjectAnimator.start();
        } else {
            pieChartView.setOuterProgress(timeProgress);
        }
        if (Math.abs(pieChartView.getInnerProgress() - completeProgress) > 0.1f) {
            innerObjectAnimator.setFloatValues(pieChartView.getInnerProgress(), completeProgress);
            innerObjectAnimator.start();
        } else {
            pieChartView.setInnerProgress(completeProgress);
        }
    }

    @Click(R.id.circle_info_frame)
    private void onClick(View v) {
        if (null == scheduleEvent) return;
        if(getActivity() instanceof DetailActivity) return;
        startActivity(DetailActivity.newIntent(getActivity(), scheduleEvent, BaseActivity.fingerUpX, BaseActivity.fingerUpY));
        getActivity().overridePendingTransition(0, 0);
    }
}
